package de.unifa.elostplugin.properties;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

public class ElostPropertyPage extends PropertyPage {

	private Text svnUserText;
	private Text svnPasswordText;

	public ElostPropertyPage() {

		super();

	}

	private void addFirstSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);

		// Label for path field
		Label svnUserLabel = new Label(composite, SWT.NONE);
		svnUserLabel.setText(PropertyConstants.L_SVNUSER);
		svnUserText = new Text(composite, SWT.NONE);
		GridData gd_svnUserText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_svnUserText.widthHint = 254;
		svnUserText.setLayoutData(gd_svnUserText);

		Label svnPasswordLabel = new Label(composite, SWT.NONE);
		svnPasswordLabel.setText(PropertyConstants.L_SVNPASSWORD);
		svnPasswordText = new Text(composite, SWT.PASSWORD);
	}

	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		addFirstSection(composite);
		initValues();
		return composite;
	}

	private Composite createDefaultComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.widthHint = 345;
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		return composite;
	}

	private boolean initValues() {
		try {
			IResource resource = ((IJavaProject) this.getElement())
					.getResource();

			String svnUserString = resource
					.getPersistentProperty(new QualifiedName("",
							PropertyConstants.P_SVNUSER));
			String svnPasswordString = resource
					.getPersistentProperty(new QualifiedName("",
							PropertyConstants.P_SVNPASSWORD));

			if (svnUserString != null) {
				svnUserText.setText(svnUserString);
			}
			if (svnPasswordString != null) {
				svnPasswordText.setText(svnPasswordString);
			}

		} catch (CoreException e) {
			return false;
		}
		return true;
	}

	private boolean saveValues() {
		try {
			IResource resource = ((IJavaProject) this.getElement())
					.getResource();
			resource.setPersistentProperty(new QualifiedName("",
					PropertyConstants.P_SVNUSER), svnUserText.getText());
			resource.setPersistentProperty(new QualifiedName("",
					PropertyConstants.P_SVNPASSWORD), svnPasswordText.getText());
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

	public boolean performOk() {
		return saveValues();
	}

}